package com.example.carconsumer.controller.hkf;

import com.baidu.aip.ocr.AipOcr;
import org.json.JSONObject;
import com.alibaba.dubbo.config.annotation.Reference;
import com.example.carinterface.pojo.OwnerCard;
import com.example.carinterface.service.hkf.OwnerCardService;
import com.example.carinterface.utils.ResultJson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@CrossOrigin
@Api(value = "用户端证件识别接口", tags = "证件识别接口API")
@RestController
@RequestMapping("/user")
public class DistinguishController {
    @Reference(check = false)
    private OwnerCardService ownerCardService;

    @PostMapping("/drivingLicense")
    @ApiOperation(value = "行驶证识别 carEngineNumber:发动机号码，licensePlateNumber：号码牌，ownerName：车主名字，carIdentifierCode：识别代码", responseReference = "carEngineNumber:发动机号码，licensePlateNumber：号码牌，ownerName：车主名字，carIdentifierCode：识别代码", httpMethod = "post", response = ResultJson.class)
    public ResultJson<Map> drivingLicense(MultipartFile file) {
         final String APPID = "22770980";//你的 App ID
         final String APIKEY = "uCPOBoCDYxP4TbcpGf9eeuKg";//你的 Api Key
         final String SECRETKEY = "OUWfEeeKfh4b4CjOWPWaGALe0M501PWF";
         ResultJson<Map> resultJson=new ResultJson();
         Map map =new HashMap();
        try {
            if (file != null) {
                byte[] flies = new byte[0];//获取图片的二进制数组
                try {
                    flies = file.getBytes();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                HashMap<String, String> options = new HashMap<String, String>();
                options.put("detect_direction", "true");
                options.put("accuracy", "normal");
                String fileName = file.getOriginalFilename();// 文件原名称
                // 将文件类型截出来，判断文件类型
                String type = fileName.indexOf(".") != -1 ? fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()) : null;
                System.out.println(type);
                if (type != null) {// 判断文件类型是否为空
                    //这里是规定图片的类型，必须是png和jpg，我这里规定的是图片的后缀，如果需要上传别的类型文件，可以自行更改
                    if ("BMP".equals(type.toUpperCase()) || "PNG".equals(type.toUpperCase()) || "JPG".equals(type.toUpperCase())) {
                        // 转存文件到指定的路径
                        AipOcr client = new AipOcr(APPID, APIKEY, SECRETKEY);
                        JSONObject res = client.vehicleLicense(flies, options);
                        System.out.println(res);
                        //    截取车牌号
                        JSONObject resultJsonObject = res.getJSONObject("words_result");
                        String carEngineNumber = resultJsonObject.getJSONObject("发动机号码").getString("words");//发动机号码
                        String licensePlateNumber = resultJsonObject.getJSONObject("号牌号码").getString("words");
                        String ownerName = resultJsonObject.getJSONObject("所有人").getString("words");//所有人
                        String carIdentifierCode = resultJsonObject.getJSONObject("车辆识别代号").getString("words");//车辆识别代号
                        String cartype=resultJsonObject.getJSONObject("车辆类型").getString("words");
                        if (carEngineNumber==null||carEngineNumber.equals("")||
                                licensePlateNumber==null||licensePlateNumber.equals("")||
                                ownerName==null||ownerName.equals("")||
                                carIdentifierCode==null||carIdentifierCode.equals("")||
                                cartype==null||cartype.equals("")){ return resultJson.setFail("有未识别到的信息，请重新上传");}
                        if (cartype.equals("小型轿车")){
                            map.put("carEngineNumber",carEngineNumber);
                            map.put("licensePlateNumber",licensePlateNumber);
                            map.put("ownerName",ownerName);
                            map.put("carIdentifierCode",carIdentifierCode);
                            resultJson.setSuccess("识别成功",map);
                        return  resultJson;}
                        else {
                            return resultJson.setFail("只能添加小型轿车");
                        }
                    }else {return  resultJson.setFail("图片格式不正确，请上传 bmp png jpg");}
                } else {
                    return resultJson.setFail("未知文件类型");
                }
            } else {
                return  resultJson.setFail("文件为空");
            }

        } catch (Exception e) {
            return resultJson.setFail("请上传正确行驶证");

        }
    }
    @PostMapping("/idCardLicense")
    @ApiOperation(value = "身份证识别", responseReference = "身份证识别", httpMethod = "post", response = ResultJson.class)
    public ResultJson<Map> idCardLicense(MultipartFile file) {
        final String APPID = "22772125";//你的 App ID
        final String APIKEY = "obvSHaOpbcbeMmWZGlkimaoN";//你的 Api Key
        final String SECRETKEY = "yQcgNwF2T2DNKjwVcyZ7099Yt2VCYv6u";
        ResultJson<Map> resultJson=new ResultJson();
        Map map =new HashMap();
        try {
            if (file != null) {
                byte[] flies = new byte[0];//获取图片的二进制数组
                try {
                    flies = file.getBytes();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                HashMap<String, String> options = new HashMap<String, String>();
                options.put("detect_direction", "false");
                // 是否检测风险
                options.put("detect_risk", "false");
                // 正反面front /back
                String idCardSide = "front";
                String fileName = file.getOriginalFilename();// 文件原名称
                // 将文件类型截出来，判断文件类型
                String type = fileName.indexOf(".") != -1 ? fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()) : null;
                System.out.println(type);
                if (type != null) {// 判断文件类型是否为空
                    //这里是规定图片的类型，必须是png和jpg，我这里规定的是图片的后缀，如果需要上传别的类型文件，可以自行更改
                    if ("BMP".equals(type.toUpperCase()) || "PNG".equals(type.toUpperCase()) || "JPG".equals(type.toUpperCase())) {
                        // 转存文件到指定的路径
                        AipOcr client = new AipOcr(APPID, APIKEY, SECRETKEY);
                        JSONObject res = client.idcard(flies, idCardSide, options);
                        System.out.println(res);
                        //    截取车牌号
                        JSONObject resultJsonObject = res.getJSONObject("words_result");
                        String name = resultJsonObject.getJSONObject("姓名").getString("words");//发动机号码
                        String idCard = resultJsonObject.getJSONObject("公民身份号码").getString("words");//所有人
                        if (name==null||name.equals("")||idCard==null||idCard.equals("")){
                            return resultJson.setFail("未识别到姓名和身份证号码，请重新上传");
                        }

                            map.put("name",name);
                            map.put("idCard",idCard);
                            resultJson.setSuccess("识别成功",map);
                            return  resultJson;
                    }else {return  resultJson.setFail("图片格式不正确，请上传 bmp png jpg");}
                } else {
                    return resultJson.setFail("未知文件类型");
                }
            } else {
                return  resultJson.setFail("文件为空");
            }

        } catch (Exception e) {
            return resultJson.setFail("请上传正确身份证");
        }
    }


}

